package com.akjava.lib.android.pool;

import java.util.ArrayList;

public class ObjectPool {

	ArrayList<Object> returnedObject=new ArrayList<Object>();
	ArrayList<Object> borrowedObject=new ArrayList<Object>();
	
	IFactory factory;
	public ObjectPool(IFactory factory){
		this.factory=factory;
	}
	
	
	public Object borrowObject() throws Exception{
		synchronized(this){
		if(returnedObject.size()==0){
			Object newOne=factory.makeObject();
			borrowedObject.add(newOne);
			return newOne;
		}else{
			Object newOne=returnedObject.remove(0);
			borrowedObject.add(newOne);
			return newOne;
		}
		}
	}
	
	public void returnObject(Object object) throws Exception{
		synchronized(this){
		factory.destroyObject(object);
		returnedObject.add(object);
		}
	}
	
}
